/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.db.mdm.mdmrepo;

import com.db.mdm.MDMConnection;
import com.db.mdm.exception.MDMDBException;
import com.db.mdm.mssql.MSSQLConnection;
import com.db.mdm.mysql.MySQLConnection;
import org.apache.commons.configuration.XMLConfiguration;

/**
 *
 * @author srkrishnan
 */
public class MDMRepo implements Runnable {

    protected XMLConfiguration config;
    protected MDMConnection repoConnection;

    /**
     * Get the value of repoConnection
     *
     * @return the value of repoConnection
     */
    public MDMConnection getRepoConnection() {
        return repoConnection;
    }

    /**
     * Set the value of repoConnection
     *
     * @param repoConnection new value of repoConnection
     */
    public void setRepoConnection(MDMConnection repoConnection) {
        this.repoConnection = repoConnection;
    }

    /**
     * Get the value of config
     *
     * @return the value of config
     */
    public XMLConfiguration getConfig() {
        return config;
    }

    /**
     * Set the value of config
     *
     * @param config new value of config
     */
    public void setConfig(XMLConfiguration config) {
        this.config = config;
    }

    public MDMRepo(XMLConfiguration config) {
        this.config = config;
    }



    public void run() {
        /* Identify database type of hub and create connection*/
        if (config.getString("repo.type").equalsIgnoreCase("mysql")) {
            repoConnection = new MySQLConnection(config.getString("repo.name"),config.getString("repo.host"), config.getString("repo.user"), config.getString("repo.pass"), config.getString("repo.db"), config.getInt("repo.port"));
            repoConnection.connectDB();
        }else if (config.getString("hub.type").equalsIgnoreCase("mssql")) {
            repoConnection = new MSSQLConnection(config.getString("repo.name"),config.getString("repo.host"), config.getString("repo.user"), config.getString("repo.pass"), config.getString("repo.db"), config.getInt("repo.port"));
            repoConnection.connectDB();
    } else {
            try {
                throw new MDMDBException("Database type not Supported.");
            } catch (MDMDBException ex) {
                ex.printStackTrace();
            }
        }

        /*create repository table*/
        repoConnection.createRepoTables(repoConnection);

    }
}
